<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Read from file</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="splfileobject.fputcsv.html">« SplFileObject::fputcsv</a></li>
      <li style="float: right;"><a href="splfileobject.fscanf.html">SplFileObject::fscanf »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.splfileobject.html">SplFileObject</a></li>
    <li>Read from file</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="splfileobject.fread" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">SplFileObject::fread</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.5.11, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">SplFileObject::fread</span> &mdash; <span class="dc-title">Read from file</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-splfileobject.fread-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>SplFileObject::fread</strong></span>(<span class="methodparam"><span class="type">int</span> <code class="parameter">$length</code></span>): <span class="type"><span class="type">string</span>|<span class="type"><span class="type false">false</span></span></span></div>

  <p class="para rdfs-comment">
   Reads the given number of bytes from the file.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-splfileobject.fread-parameters">
  <h3 class="title">参数</h3>
  <dl>
   
    <dt>
<code class="parameter">length</code></dt>

    <dd>

     <p class="para">
      The number of bytes to read.
     </p>
    </dd>

   
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-splfileobject.fread-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns the string read from the file  或者在失败时返回 <strong><code>false</code></strong>.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-splfileobject.fread-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="splfileobject.fread.examples.basic">
    <p><strong>示例 #1 <span class="methodname"><strong>SplFileObject::fread()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Get&nbsp;contents&nbsp;of&nbsp;a&nbsp;file&nbsp;into&nbsp;a&nbsp;string<br /></span><span style="color: #0000BB">$filename&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"/usr/local/something.txt"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$file&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">SplFileObject</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"r"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$contents&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$file</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fread</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getSize</span><span style="color: #007700">());<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-splfileobject.fread-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    Note that <span class="methodname"><strong>SplFileObject::fread()</strong></span> reads from the current
    position of the file pointer. Use <span class="methodname"><a href="splfileobject.ftell.html" class="methodname">SplFileObject::ftell()</a></span>
    to find the current position of the pointer and <span class="methodname"><a href="splfileobject.rewind.html" class="methodname">SplFileObject::rewind()</a></span>
    (or <span class="methodname"><a href="splfileobject.fseek.html" class="methodname">SplFileObject::fseek()</a></span>) to rewind the pointer position.
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-splfileobject.fread-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.fread.html" class="function" rel="rdfs-seeAlso">fread()</a> - 读取文件（可安全用于二进制文件）</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>